package metier.sess;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import metier.entities.Station;
import metier.entities.Transport;


@Stateless(name="Station")
public class StationEJBImpl implements IStationLocal{
	@PersistenceContext(unitName="PU_Cargaison")
	private EntityManager em;

	@Override
	public void addStation(Station t) {
		em.persist(t);
		
	}

	@Override
	public List<Station> getAllStation(Integer type) {
		String a="";
		
		if(type==0)
		{
			a="select g from Gare g";
		}
		else if(type==1)
		{
			a="select p from Port p";
		}
		else
		{
			a="select p from Airport p";
		}
		
		
		Query req=em.createQuery(a);
		return req.getResultList();
		
	}

	@Override
	public Station getStation(Integer Id) {
		return em.find(Station.class, Id);
	}

	@Override
	public void updateStation(Station t) {
		em.merge(t);
		
	}

	@Override
	public void deleteStation(Integer Id) {
		em.remove(getStation(Id));
		
	}

	//0 pour les gares,1 pour les ports et 2 pour les airports
	@Override
	public List<Station> getAllActiveStation(Integer type) {
		String a="";
		if(type==0)
		{
			a="select g from Gare g where g.Statut=true";
		}
		else if(type==1)
		{
			a="select p from Port p where p.Statut=true";
		}
		else
		{
			a="select p from Airport p where p.Statut=true";
		}
		Query req=em.createQuery(a);
		return req.getResultList();
	}

	//type=0 pour Gare ,type=1 pour Port et type=2 pour AirPort.location=0 pour pays ,location=1 pour Etat et location=2 pour ville.
	@Override
	public List<Station> getAllActiveStationLocation(Integer type,Integer location,Integer id) {
		String mssg="";
		String mssg1="";
		if(type==0)
			mssg="  Gare";
		if(type==1)
			mssg="  Port";
		if(type==2)
			mssg="  Airport";
		
		if(location==0)
			mssg1=" and s.villeStations.etat.pays.IdPays="+id;
		if(location==1)
			mssg1=" and s.villeStations.etat.IdEtat="+id;
		if(location==2)
			mssg1=" and s.villeStations.IdVille="+id;
		
		
		Query req=em.createQuery("select s from "+mssg+" s where s.Statut=true "+mssg1);
		
		System.out.println("listeGareP1"+req.getResultList().size());
		return req.getResultList();
	}
	
	

}
